<?php
defined('SYSPATH') or die('No direct script access.') ;
/**
 * @author Y.N
 *  2011/5/9
 */
class Cli_Merge extends CLI
{

	public function main()
	{
		//self::merge_same_spu();
		self::merge_not_spu();
	}



	/**
	 * 修订数据
	 *
	 * @param	none
	 * @return	void
	 * @update	2011/12/20
	 */
	public static function fixed_data()
	{
		$brands = array();
		$brands_str = array();

		$products = DB::select('product_id', 'props', 'props_str', 'cid')
			->from('products')
			->where('status', '=', 3)
			->where('is_new', '=', 2)
			->where('props', 'REGEXP', '[0-9]+:[0-9]+;')
			->fetch_all();
		foreach ($products as $p)
		{
			// 清除无商品SPU
			$count = DB::select(DB::expr('count(0)'))
				->from('goods')
				->where('product_id', '=', $p['product_id'])
				->fetch_one();
			if ( ! $count)
			{
				DB::delete('products')
					->where('product_id', '=', $p['product_id'])
					->execute();
				continue;
			}
			$brands = array();
			$brands_str = array();

			$props = explode(';', $p['props']);
			foreach ($props as $k => $ps)
			{
				if ($k>2)
				{
					break;
				}
				$explode = explode(':', $ps);
				$pid = current($explode);
				$vid = next($explode);
				// 查找属性名称
				$p_s = DB::select('name_alias', 'parent_pid')
					->from('item_props_view')
					->where('pid', '=', $pid)
					->where('cid', '=', $p['cid'])
					->fetch_row();
				// 查找属性值名称
				$v_name = DB::select('name_alias')
					->from('item_values_view')
					->where('pid', '=', $pid)
					->where('vid', '=', $vid)
					->where('cid', '=', $p['cid'])
					->fetch_one();
				if ( ! $p_s['parent_pid'])
				{
					array_unshift($brands_str, $p_s['name_alias'].':'.$v_name);
					array_unshift($brands, $ps);
				} else {
					$brands_str[] = $p_s['name_alias'].':'.$v_name;
					$brands[] = $ps;
				}
			}

			$update = array(
				'props' => implode(';', $brands),
				'props_str' => implode(';', $brands_str),
			);
			DB::update('products')
				->set($update)
				->where('product_id', '=', $p['product_id'])
				->execute();
			unset($update, $brands, $brands_str);
			CLI::output('>');
		}
		CLI::output('产品修订完毕');
	}


	/**
	 * 非SPU统计
	 *
	 * @param	none
	 * @return	void
	 * @update	2011/10/14
	 */
	public static function merge_not_spu()
	{

		// 查找没有SPU的商品
		$goods = DB::select('goods_id', 'cid', 'props', 'goods_title', 'image_md5')
			->from('goods')
			->where('product_id', '=', 0)
			->where('deleted', '=', 0)
			->where('props', '<>', '')
			->fetch_all();

		foreach ($goods as $g)
		{
			$product_id = 0;
			$binds = array();
			$props = explode(';', $g['props']);
			$brands = array();

			$binds_str = array();
			$brands_str = array();

			foreach ($props as $p)
			{
				$explode = explode(':', $p);
				$pid = current($explode);
				$vid = next($explode);

				// 查找属性名称
				$p_s = DB::select('name_alias', 'parent_pid')
					->from('item_props_view')
					->where('pid', '=', $pid)
					->where('cid', '=', $g['cid'])
					->fetch_row();
				// 查找属性值名称
				$v_name = DB::select('name_alias')
					->from('item_values_view')
					->where('pid', '=', $pid)
					->where('vid', '=', $vid)
					->where('cid', '=', $g['cid'])
					->fetch_one();

				// 是否非关键属性
				$is_fit = DB::select('is_key_prop')
					->from('item_props_view')
					->where('is_price_prop', '=', 0)
					->where('is_color_prop', '=', 0)
					->where('pid', '=', $pid)
					->where('cid', '=', $g['cid'])
					->fetch_one();
				if ( ! $is_fit)
				{
					$binds_str[] = $p_s['name_alias'].':'.$v_name;
					$binds[] = $p;
				} else {
					// 是否子属性
					if ( ! $p_s['parent_pid'])
					{
						array_unshift($brands_str, $p_s['name_alias'].':'.$v_name);
						array_unshift($brands, $p);
					} else {
						$brands_str[] = $p_s['name_alias'].':'.$v_name;
						$brands[] = $p;
					}
				}
			}

			// 定义是否包含其他属性或属性值开关
			$go = true;

			foreach ($brands as $k => $b)
			{
				$explode = explode(':', $b);
				$pid = current($explode);
				$vid = next($explode);

				$name_alias = DB::select('name_alias')
					->from('item_values_view')
					->where('pid', '=', $pid)
					->where('cid', '=', $g['cid'])
					->where('vid', '=', $vid)
					->fetch_one();

				if (stristr($name_alias, '其他') OR stristr($name_alias, '其它'))
				{
					// 关闭开关
					$go = false;
				}
			}
			$brand_data = implode(';', $brands);

			$binds_data = implode(';', $binds);
			if (count($brands)>1)
			{
				$product_id = (int) DB::select('product_id')
					->from('products')
					->where('props', '=', $brand_data)
					->where('cid', '=', $g['cid'])
					->where('status', '=', 3)
					->where('is_new', '<>', 2)
					->fetch_one();
			}
			if ( ! $product_id AND $go AND count($brands)>1)
			{
				$product_id = (int) DB::select('product_id')
					->from('products')
					->where('props', '=', $brand_data)
					->where('cid', '=', $g['cid'])
					->where('status', '=', 3)
					->where('is_new', '=', 2)
					->order_by('product_id', 'asc')
					->fetch_one();
			}

			if ( ! $product_id AND ! empty($binds) AND count($binds)>2 AND  ! $go)
			{

				$product_id = (int) DB::select('product_id')
					->from('products')
					->where_open()
					->where('binds', 'REGEXP', '^'.$binds_data)
					->or_where('product_name', '=', trim($g['goods_title']))
					->where_close()
					->where('cid', '=', $g['cid'])
					->where('status', '=', 3)
					->where('is_new', '=', 2)
					->order_by('product_id', 'asc')
					->fetch_one();
			}
			if ( ! $product_id AND count($binds)>2)
			{
				// 获取类目名称
				$c_name = DB::select('name')
					->from('item_cats')
					->where('cid', '=', $g['cid'])
					->fetch_one();

				// 插入的数据
				$products = array(
					'cid' => $g['cid'],
					'cat_name' => $c_name,
					'product_name' => $g['goods_title'],
					'image_md5' => $g['image_md5'],
					'props' => $brand_data,
					'props_str' => implode(';', $brands_str),
					'binds_str' => implode(';', $binds_str),
					'binds' => $binds_data,
					'created' => date('2008-m-d H:i:s'),
					'status' => 3,
					'is_new' => 2,
				);

				if ( ! $go)
				{
					$products['props'] = '';
					$products['props_str'] = '';
				}

				// 新增SPU
				$last = DB::insert('products', array_keys($products))
					->values(array_values($products))
					->execute();
				$product_id = current($last);

				// 获取商品图片
				$images = DB::select('image_md5', 'is_zoom')
					->from('goods_images')
					->where('goods_id', '=', $g['goods_id'])
					->limit(2)
					->fetch_all();
				foreach ($images as $img)
				{
					// 插入产品图片库
					$insert = array(
						'product_id' => $product_id,
						'image_md5' => $img['image_md5'],
						'created' => time(),
						'is_zoom' => $img['is_zoom'],
					);
					DB::insert('product_imgs', array_keys($insert))
						->values(array_values($insert))
						->execute();
				}
			}
			if ($product_id)
			{
				DB::update('goods')
					->set(array('product_id' => $product_id))
					->where('cid', '=', $g['cid'])
					->where('goods_id', '=', $g['goods_id'])
					->execute();
				CLI::output($g['goods_id']."更新完毕\n");
			}
			unset($insert, $brands, $brands_str, $binds, $binds_str, $products);
		}
		CLI::output('非SPU合并完毕');
	}


	/**
	 * 合并相同SPU
	 *
	 * @param	none
	 * @return	void
	 * @update	2011/12/20
	 */
	public static function merge_same_spu()
	{
		// 查找存在相同SPU的
		$goods = DB::select('product_id', 'shop_id', 'retail_price')
			->from('goods')
			->where('deleted', '=', 0)
			->where('product_id', '<>', 0)
			->group_by('shop_id')
			->group_by('product_id')
			->having(DB::expr('count(0)'), '>', 1)
			->fetch_all();

		foreach ($goods as $g)
		{
			$prices = array();
			$g_goods = DB::select('retail_price')
				->from('goods')
				->where('product_id', '=', $g['product_id'])
				->where('shop_id', '=', $g['shop_id'])
				->where('deleted', '=', 0)
				->fetch_all();
			$cert_type = DB::select('cert_type')
				->from('shops')
				->where('shop_id', '=', $g['shop_id'])
				->where('deleted', '=', 0)
				->where('status', '=', 4)
				->fetch_one();

			foreach ($g_goods as $gg)
			{
				$prices[] = $gg['retail_price'];
			}
			$min_price = min($prices);
			$max_price = max($prices);

			$set = array(
				'on_sale' => 0,
			);
			// 原厂
			if ($cert_type == 1)
			{
				DB::update('goods')
					->set($set)
					->where('product_id', '=', $g['product_id'])
					->where('retail_price', '<>', $min_price)
					->where('shop_id', '=', $g['shop_id'])
					->execute();
			} else {
				DB::update('goods')
					->set($set)
					->where('product_id', '=', $g['product_id'])
					->where('retail_price', '<>', $max_price)
					->where('shop_id', '=', $g['shop_id'])
					->execute();
			}
			CLI::output('>');
		}
	}
}